#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.test_logic.RequestBase import RequestBase


class REDFISH_USER_002(Case):
    """
        Name:
            REDFISH_USER_002
        Label:
            查询指定用户信息
        PreCondition:
            BMC web service normal
        TestStep:
            1)  1.get https://BMC_IP/redfish/v1/AccountService/Accounts/account_id
        ExpectedResult:
            A) 1.执行响应码为200,查看指定用户信息（与iBMC-用户&安全- 本地用户，指定用户下信息一致）
        Parameters:

        Author:
            baixin liuya
        Last Modified:

        Remark:

    """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)
        self.bmc_ip = self.bmc_host.local_ip
        self.bmc_user = self.bmc_host.username
        self.bmc_pwd = self.bmc_host.password

        self.logger.info("获取Token")
        self.request = RequestBase(self.bmc_ip, self.bmc_user, self.bmc_pwd)
        return_code, session_id = self.request.create_session()
        self.assertIsNotNone(return_code)
        self.session_id = session_id

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 获取所有的用户信息")
        dst_url = "/redfish/v1/AccountService/Accounts"
        resp = self.request.get(dst_url)
        self.assertIsNotNone(resp[0])
        content = resp[1]
        self.logger.info(f"接口[{dst_url}]返回值：\n{json.dumps(content, indent=4, ensure_ascii=False)}")
        user_count = content['Members@odata.count']
        self.log_step(f"Step2: 用户数：{user_count}")
        user_ids = []
        for i in range(user_count):
            user_id = content['Members'][i]['Id']
            user_ids.append(user_id)

        for id in user_ids:
            url = f"/redfish/v1/AccountService/Accounts/{id}"
            resp = self.request.get(dst_url)
            self.assertIsNotNone(resp[0])
            content = resp[1]
            self.logger.info(f"接口[{url}]返回值：\n{json.dumps(content, indent=4, ensure_ascii=False)}")

    # 测试结束后恢复环境
    def post_test_case(self):
        resp = self.request.delete_session(self.session_id)
        self.assertIsNotNone(resp[0], "清除Token失败")
        self.logger.info("清除Token成功")
